<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>菜单管理--右边列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />
</head>
<body class="childrenBody">

<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <ul class="layui-tab-title">
        <li >就诊历史信息</li>
        <li class="layui-this">医嘱信息</li>

    </ul>

    <div class="layui-tab-content">
        <!--病历信息开始-->
        <div class="layui-tab-item ">
            <iframe name="x" width="100%" height="800" frameborder="0">
            </iframe>
        </div>
        <!--病历信息结束-->
        <!--医嘱信息开始-->
        <div class="layui-tab-item layui-show">
        <blockquote class="layui-elem-quote"  style="margin-top: 5px;">基本信息</blockquote>
            <div style="padding: 5px" id="patientinfo">
                <form action="" method="post" class="layui-form layui-form-pane" id="patientdataFrm" lay-filter="patientdataFrm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">档案编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="filenum" autocomplete="off"
                                   placeholder="请输入档案编号" class="layui-input" style="width: 75.4%">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">*姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="patientname" lay-verify="required" autocomplete="off" placeholder="请输入姓名" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="sex" value="1" title="男" >
                                <input type="radio" name="sex" value="0" title="女" >
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">*年龄</label>
                            <div class="layui-input-inline">
                                <input type="text" name="age" lay-verify="required" autocomplete="off" placeholder="请输入年龄" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">身份证号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="idcardnum" lay-verify="required" autocomplete="off" placeholder="请输入身份证号" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系电话</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" lay-verify="required" autocomplete="off" placeholder="请输入联系电话" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">体重(KG)</label>
                            <div class="layui-input-inline">
                                <input type="text" name="weight" autocomplete="off" placeholder="请输入体重" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">出生日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="birth" readonly="readonly"  id="birth" autocomplete="off" placeholder="请输入出生日期" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">紧急联系人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="contacts" autocomplete="off" placeholder="请输入紧急联系人" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">紧急联系人电话</label>
                            <div class="layui-input-inline">
                                <input type="text" name="contactsphone" autocomplete="off" placeholder="请输入紧急联系人电话" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" autocomplete="off"
                                   placeholder="请输入公司地址" class="layui-input" style="width: 75.4%">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="remarks" autocomplete="off"
                                   placeholder="请输入备注" class="layui-input" style="width: 75.4%">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">过敏史</label>
                            <div class="layui-input-inline">
                                <input type="radio" name="allergy" value="1" title="是" >
                                <input type="radio" name="allergy" value="0" title="否" checked="">
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <blockquote class="layui-elem-quote"  style="margin-top: 5px;">医嘱信息</blockquote>
            <div style="padding: 5px" id="treatinfo">
                <form action="" method="post" class="layui-form layui-form-pane" id="treatdataFrm" lay-filter="treatdataFrm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">输入诊断</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="jiuzhenid" id="jiuzhenid" th:value="${jiuzhenid}"><!--诊断表id-->
                            <input type="text" name="zhenduan" autocomplete="off"
                                   placeholder="请输入诊断,下拉" class="layui-input" style="width: 75.4%">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">医嘱填写</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" name="treatcontent" id="treatcontent"></textarea>
                        </div>
                    </div>
                    <!-- 数据表格开始 -->
                    <div>
                        <table class="layui-hide" id="treatTable" lay-filter="treatTable"></table>
                        <div id="treatToolBar" style="display: none;">
                            <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加</button>
                            <button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>批量删除</button>
                        </div>
                        <div id="treatRowBar" style="display: none;">
                            <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
                        </div>
                    </div>
                    <!-- 数据表格结束 -->
                    <blockquote class="layui-elem-quote"  style="margin-top: 5px;">
                        <h1 >总金额: <input type="text" id="totalsum" disabled="true" name="totalsum"
                                       style="text-align: center;border: none;BACKGROUND-COLOR: transparent;width: 90px">元</h1>
                    </blockquote>
                    <div style="text-align: center">
                        <button  type="button" class="layui-btn layui-btn-lg" lay-submit="" lay-filter="doRecordSubmit" id="doRecordSubmit" >保存</button>
                        <button  type="button" class="layui-btn layui-btn-lg" lay-submit="" lay-filter="doRecordSubmitUpdate" name="doRecordSubmitUpdate" >保存修改</button>
                        <button  type="button" id="dayinchufang" class="layui-btn layui-btn-lg" >打印处方</button>
                        <button  type="button" id="shoufei" lay-submit="" lay-filter="shoufei" class="layui-btn layui-btn-danger layui-btn-lg" >收费</button>
                        <button  type="button" id="jieshujiuzhen"  lay-submit="" lay-filter="OverMenzhenSubmit"  class="layui-btn layui-btn-danger layui-btn-lg" >结束就诊</button>
                    </div>
                </form>
            </div>
        </div>
        <!--医嘱信息结束-->
    </div>
</div>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 10px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">药品名称</label>
                <input type="hidden" name="patientid" id="patientid">
                <input type="hidden" name="patientidtoinfo" id="patientidtoinfo" th:value="${patientid}">
                <input type="hidden" name="goodslistnum" id="goodslistnum" th:value="${goodslistnum}">
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid" lay-filter="goodsid" lay-search="">
                       <!-- <option value="0">请选择药品</option>-->
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">药品类型</label>
                <div class="layui-input-inline">
                    <select name="goodstypeid" id="goodstypeid" lay-filter="goodstypeid" lay-search="">
                       <!-- <option value="0">请选择药品类型</option>-->
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开药天数</label>
                <div class="layui-input-inline">
                    <input type="number" class="layui-input"  name="day" lay-verify="required|isNum" placeholder="请输入开药天数">
                </div>
            </div>
           <!-- <div class="layui-inline">
                <label class="layui-form-label">数量</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input"  name="number" lay-verify="required|isNum" placeholder="请输入开药天数">
                </div>
            </div>-->
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input"  name="remark"  placeholder="备注">
                </div>
            </div>
        </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<script type="text/javascript" src="/resources/layui/layui.js"></script>

<script type="text/javascript">
    var tableIns;
    layui.use(['jquery','form','table','layer','element'],function(){
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var element=layui.element;
        $("#doRecordSubmitUpdate").attr("style","display:none;");
        var patientidtoinfo = $("#patientidtoinfo").val();//提取患者id
        var goodslistnum = $("#goodslistnum").val();//提取患者id
        window.frames["x"].location.href="/opd/toRecordManager?patientid=" + patientidtoinfo;
        //初始化基本信息
        if(patientidtoinfo != 0 ){
            $.get("/patient/loadAllPatient",{id:patientidtoinfo},function(res){
              //  var res = res.data[0];
                $("#patientid").val(res.data[0].id);
                //装载新的数据
                form.val("patientdataFrm",res.data[0]);
                form.render();
            });
            $("#patientidtoinfo").val("");
        }else{
            $('button').attr("disabled",true);
        }
        //加载 数据
        //修理项目==》Table
        //在使用table之前加上下面这句就可以了
        tableIns=table.render({
            elem:'#treatTable'
            ,url:'/sales/loadAllSalesInMenZhen'//sales的加载门诊药单
            ,where:{patientid:patientidtoinfo,goodslistnum:goodslistnum}
            //,data:array
            ,toolbar: '#treatToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '西/成药处方'
            ,totalRow: true
            //,height:'full-80'
            ,cols: [ [
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'销售ID',align:'center' ,width:'80',hide:true}
                ,{field:'customername', title:'患者id(隐藏)',align:'center',width:'120',hide:true}
                ,{field:'goods', title:'药品名称',align:'center',width:'120',templet:function (res) {
                        return '<span>'+res.goods.goodsname+'</span>';}}
                ,{field:'goods', title:'药品规格',align:'center',width:'120',templet:function (res) {
                        return '<span>'+res.goods.size+'</span>';}}
                ,{field:'goods', title:'单次用量',align:'center',width:'80',templet:function (res) {
                        return '<span>'+res.goods.oneuse+'</span>';}}
                ,{field:'goodstypename', title:'使用方法',align:'center',width:'80'}
                ,{field:'goods', title:'使用频率',align:'center',width:'120',templet:function (res) {
                        return '<span>1日'+res.goods.userate+'次</span>';}}
                ,{field:'goods', title:'单价',align:'center',width:'80',templet:function (res) {
                        return '<span>'+res.goods.price+'</span>';}}
                ,{field:'day', title:'开药天数',align:'center',width:'100'}
                ,{field:'number', title:'数量',align:'center',width:'100',totalRowText: '处方金额:'}
                ,{field:'sumprice', title:'金额',align:'center',width:'100',totalRow: true}
                ,{field:'ispay', title:'是否缴费',align:'center',width:'100',templet:function(d){
                        return d.ispay==1?'<font color=red>未缴费</font>':'<font color=blue>已缴费</font>';
                    }}
                ,{field:'remark', title:'备注',align:'center',width:'120'}
                ,{field:'operateperson', title:'操作员',align:'center',width:'100',hide:true}
                ,{fixed: 'right', title:'操作', toolbar: '#treatRowBar',align:'center',width:'100'}
            ] ],done: function(res, curr, count){ //处理删除某一页最后一条数据的BUG
                //console.log(res);
                let sum = 0.0;
                let totalsum = $("#totalsum");
                for (let i = 0; i < res.data.length; i++) {
                    sum += res.data[i].sumprice;
                }
               // console.log(ids);
                totalsum.val(sum);
            }
        });

        //监听工具条的事件
        table.on("toolbar(treatTable)",function(obj){
            switch(obj.event){
                case 'add':
                   treatsalesadd();
                    break;
                case 'batchDelete':
                    deleteSalesAll();
                    break;
            };
        });

        //监听行工具条的事件
        table.on("tool(treatTable)",function(obj){
            var data = obj.data; //获得当前行整个对象的数据
            switch(obj.event){
                case 'delete':
                    deleteSales(data);
                    break;
            };
        });

        var mainIndex;
        var url;
        //添加药单
        function treatsalesadd() {
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['1020px','450px'],
                title:'添加药单',
                success:function(){
                    $("#dataFrm")[0].reset();
                    url="/sales/addMenzhenSales";
                    initGoodsSelect(0,0);
                    initGoodstypeSelect(0);
                    $(".mydiv").hide();//删除不要修改的div框架样式
                }
            });
        }
        //加载药品下拉列表
          function initGoodsSelect(goodsid,goodstypeid){
              $.get("/goods/loadAllGoods",{goodstypeid:goodstypeid},function(res){
                  var data=res.data;
                  var dom=$("#goodsid");
                  dom.html("");
                  var html='<option value="0">请选择药品</option>'
                  $.each(data,function(index,item){
                      html+='<option value="'+item.id+'">'+item.goodsname+'-'+item.size+'['+item.providername+']</option>'
                  });
                  dom.html(html);
                  //如果goodsid有值就反选
                  if(goodsid!==undefined){
                      dom.val(goodsid);
                  }
                  form.render("select");
              });
          }
        //监听添加页面的供应商下拉框的改变事件
        form.on('select(goodstypeid)', function(data){
            var goodstypeid=data.value;
            initGoodsSelect(0,goodstypeid);
        });
        //加载药品类型下拉列表
        function initGoodstypeSelect(goodstypeid){
            $.get("/goodstype/loadAllGoodstype",function(res){
                var data=res.data;
                var dom=$("#goodstypeid");
                dom.html("");
                var html='<option value="0">请选择药品类型</option>'
                $.each(data,function(index,item){
                    html+='<option value="'+item.id+'">'+item.goodstypename+'</option>'
                });
                dom.html(html);
                //如果goodsid有值就反选
                if(goodstypeid!==undefined){
                    dom.val(goodstypeid);
                }
                form.render("select");
            });
        }



        //修改或增加提交
        form.on("submit(doSubmit)",function(data){
       //增加sales表格数据
          //  alert(JSON.stringify(data.field));
            if(data.field.patientid == ""){
                layer.msg("没有就诊病人");
            }else{
            $.post(url,data.field,function(res){
                if(res.code===200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            }
            return false;
        });

        //病例数据提交
        form.on("submit(doRecordSubmit)",function(data){//这里的data要用data.field
            if(data.field.zhenduan == "" || data.field.totalsum == 0 ){
                layer.confirm("诊断不能为空或药单不能为空");
            }else{
                layer.confirm('你确定要保存吗?', {icon: 3, title:'提示'}, function(index){
                    //修改jiuzhen表格的totalsum金额数据
                    $.post("/jiuzhen/updateJiuzhen",{id:data.field.jiuzhenid,totalsum:data.field.totalsum},function(res){
                        layer.msg(res.msg);
                    });
                    //添加病历表格数据
                        $.post("/record/addRecord",data.field,function(res){//这个提交方法包含修改
                            layer.msg(res.msg);
                        });
                       /* $.post("/record/updateRecord",data.field,function(res){
                            layer.msg(res.msg);
                        });*/
                    window.frames["x"].location.reload();
                    layer.close(index);
                });
            }
            return false;
        });



        //删除
        function deleteSales(data){
            layer.confirm('你确定要删除这个药品吗?', {icon: 3, title:'提示'}, function(index){
                $.post("/sales/deleteSales",{id:data.id},function(res){
                    if(res.code===200){
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }

        //批量删除
        function  deleteSalesAll(){
            //得到选中行
            var checkStatus = table.checkStatus('treatTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要删除这些药品吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids+="ids="+item.id;
                        }else{
                            ids+="&ids="+item.id;
                        }
                    });
                    $.post("/sales/batchDeleteSalesforMenzhen",ids,function(res){
                        if(res.code==200){
                            tableIns.reload();
                        }
                        layer.msg(res.msg);
                    })
                    layer.close(index);
                });
            }else{
                layer.msg("请选中操作行")
            }
        }
        //打印处方
        $(document).on('click','#dayinchufang',function() {
            alert("dayinchufang");
        });

        //批量修改药单为缴费状态
        form.on("submit(shoufei)",function (data) {
            if(data.field.zhenduan == "" || data.field.totalsum == 0 ){
                layer.confirm("诊断不能为空或药单不能为空");
            }else{
                //得到选中行
                var checkStatus = table.checkStatus('treatTable');
                var dataLength=checkStatus.data.length;
                if(dataLength>0){
                    layer.confirm('你确定要收费这些药品吗?', {icon: 3, title:'提示'}, function(index){
                        var data=checkStatus.data; //获取选中行的数据
                        var ids="";
                        $.each(data,function(index,item){
                            if(index==0){
                                ids+="ids="+item.id;
                            }else{
                                ids+="&ids="+item.id;
                            }
                        });
                       // alert(ids);
                        $.post("/sales/batchUpdateSalesforMenzhen",ids,function(res){
                            if(res.code==200){
                                tableIns.reload();
                            }
                            layer.msg(res.msg);
                        });
                        layer.close(index);
                    });
                }else{
                    layer.msg("请选中药品进行操作")
                }
            }
        });

        //结束就诊
        form.on("submit(OverMenzhenSubmit)",function(data){//这里的data要用data.field
            if(data.field.zhenduan == "" || data.field.totalsum == 0 ){
                layer.confirm("诊断不能为空或药单不能为空");
            }else{
                layer.confirm('你确定要结束就诊吗?结束前请确认是否保存和收费', {icon: 3, title:'提示'}, function(index){
                    //修改jiuzhen表格的结束数据
                    $.post("/jiuzhen/updateJiuzhen",{id:data.field.jiuzhenid,jiuzhenstatus:3},function(res){
                        layer.msg(res.msg);
                    });
                    window.frames.location.reload();
                    layer.close(index);
                });
            }
            return false;
        });
    });
</script>

</body>
</html>